#!/usr/bin/env ruby

_rt = ARGV.first
_rt ||= ENV['RESULT_ROOT']
_rt ||= Dir.pwd

PDEL = 10
OUTPUT_NUM = 20

def read_ftq_data(results_dir)
  data = []
  files = Dir["#{results_dir}/ftq_*.dat"]
  files.each do |file|
    rdata = File.read(file)
    n = (rdata =~ /^[^#]/)
    rdata.slice!(0, n)
    sfdata = rdata.split
    sfdata.select!.with_index { |_x, i| i.odd? }
    n = sfdata.size
    ndel = n * PDEL / 100
    sfdata.slice!(n - ndel, ndel)
    sfdata.slice!(0, ndel)
    data.concat(sfdata.map(&:to_i))
  end
  data
end

def make_histogram(data, levels)
  samples = data.size
  mean = data[data.size / 2]
  start = 0
  levels.each_with_index.map do |level, i|
    lc = mean * level / 100
    nstart = data.bsearch_index { |n| n >= lc }
    nstart ||= data.size
    num = (nstart - start) * 1_000_000.0 / samples
    start = nstart
    slevel = i.zero? ? 0 : levels[i - 1]
    [slevel, level, num]
  end
end

def print_histogram(histogram, output_num)
  histogram.sort_by! { |_sl, _el, n| -n }
  histogram.first(output_num).each do |slevel, elevel, num|
    printf "mean.%d-%d%%: %g\n", slevel, elevel, num
  end
end

def histogram_analyze(_rt)
  data = read_ftq_data("#{_rt}/results")

  data.sort!
  mean = data[data.size / 2]
  max = data.last

  printf "max: %d\n", max
  printf "mean: %d\n", mean

  perf_levels = (1..((max - 1) * 100 + mean) / mean).map { |l| l * 1 }
  perf_num_levels = make_histogram(data, perf_levels)
  print_histogram(perf_num_levels, OUTPUT_NUM)
end

histogram_analyze(_rt)
